﻿using Sidvall.Collections;
using System.Threading.Tasks;

namespace Sidvall.Net.Mail
{
    public class SmtpClientManager
    {
        #region Public Members

        public Sidvall.Net.Mail.ISmtpClient SmtpClient { get; set; }

        #region SendAsync

        public async Task SendAsync(string[] recipientAddresses, string subject, string body, bool isBodyHtml)
        {
            await SendAsync(recipientAddresses, subject, body, isBodyHtml, null, null, null, null, null, null, null).ConfigureAwait(false);
        }
        public async Task SendAsync(string[] recipientAddresses, string subject, string body, bool isBodyHtml, string fromAddress, string[] ccRecipientAddresses, string[] bccRecipientAddresses)
        {
            await SendAsync(recipientAddresses, subject, body, isBodyHtml, fromAddress, ccRecipientAddresses, bccRecipientAddresses, null, null, null, null).ConfigureAwait(false);
        }
        public async Task SendAsync(string[] recipientAddresses, string subject, string body, bool isBodyHtml, string fromAddress, string[] ccRecipientAddresses, string[] bccRecipientAddresses, string senderAddress, string[] replyToAddresses, System.Collections.Generic.IEnumerable<Attachment> attachments, string connectionString)
        {
            var message = new MailMessage()
            {
                BccRecipientAddresses = bccRecipientAddresses.JoinToString(MailMessage.EmailAddressSeparator, true),
                Body = body,
                CCRecipientAddresses = ccRecipientAddresses.JoinToString(MailMessage.EmailAddressSeparator, true),
                FromAddress = fromAddress,
                IsBodyHtml = isBodyHtml,
                RecipientAddresses = recipientAddresses.JoinToString(MailMessage.EmailAddressSeparator, true),
                ReplyToAddresses = replyToAddresses.JoinToString(MailMessage.EmailAddressSeparator, true),
                SenderAddress = senderAddress,
                Subject = subject,
                ConnectionString = connectionString,
            };
            message.Attachments.AddRange(attachments);
            await SendAsync(message).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public async Task SendAsync(MailMessage message)
        {
            await this.SmtpClient.SendAsync(message).ConfigureAwait(false);
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "SmtpClientManager - Sidvall.Net.Mail.ISmtpClient";
        }

        #endregion

        #endregion
        #region Constructors

        public SmtpClientManager(Sidvall.Net.Mail.ISmtpClient smtpClient)
        {
            this.SmtpClient = smtpClient;
        }

        #endregion
    }
}
